package com.toycloud.business.controller.demand;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.toycloud.business.domain.bo.DemandLogBo;
import com.toycloud.business.domain.vo.DemandLogVo;
import com.toycloud.business.dto.results.UserParam;
import com.toycloud.business.service.IDemandLogService;
import com.toycloud.common.core.page.TableDataInfo;
import com.toycloud.common.core.utils.poi.RuoyiExcelUtil;
import com.toycloud.system.log.annotation.Log;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import com.toycloud.common.core.controller.BaseController;
import com.toycloud.common.core.domain.PageQuery;
import com.toycloud.common.core.domain.R;
import com.toycloud.common.core.validate.AddGroup;
import com.toycloud.common.core.validate.EditGroup;
import com.toycloud.common.core.validate.QueryGroup;
import com.toycloud.common.enums.BusinessType;


/**
 * demandLog
 *
 * @author binlu6
 * @date 2024-11-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/log")
@Slf4j
public class DemandLogController extends BaseController {

    private final IDemandLogService iDemandLogService;

    /**
     * 查询demandLog列表
     */
    @GetMapping("/list")
    public TableDataInfo<DemandLogVo> list(DemandLogBo bo, PageQuery pageQuery) {
        return iDemandLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出demandLog列表
     */
    @Log(title = "demandLog")
    @PostMapping("/export")
    public void export(DemandLogBo bo, HttpServletResponse response) {
        List<DemandLogVo> list = iDemandLogService.queryList(bo);
        RuoyiExcelUtil.exportExcel(list, "demandLog", DemandLogVo.class, response);
    }

    /**
     * 获取demandLog详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<DemandLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iDemandLogService.queryById(id));
    }

    @GetMapping("did/{did}")
    public R<List<DemandLogVo>> getList(@NotNull(message = "主键不能为空")
                                            @PathVariable Integer did){
        return R.ok(iDemandLogService.queryListBydid(did));
    }

    /**
     * 新增demandLog
     */
    @Log(title = "demandLog")
    @PostMapping()
    public R<Integer> add(HttpServletRequest request, @Validated(AddGroup.class) @RequestBody DemandLogBo bo) {
//                UserParam param =iDemandLogService.username(request);
//        bo.setCreateBy(param.getUsername());
        return R.ok(iDemandLogService.insertByBo(bo));
    }

    /**
     * 修改demandLog
     */
    @Log(title = "demandLog")
    @PutMapping()
    public R<Void> edit(HttpServletRequest request,@Validated(EditGroup.class) @RequestBody DemandLogBo bo) {
//                UserParam param =iDemandLogService.username(request);
//        bo.setCreateBy(param.getUsername());
        return toAjax(iDemandLogService.updateByBo(bo));
    }

    /**
     * 删除demandLog
     *
     * @param ids 主键串
     */
    @Log(title = "demandLog")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDemandLogService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取新版本
     */
    @GetMapping("/plusVersion")
    public R<String> plusVersion(String version){
        return R.ok(iDemandLogService.plusVersion(version));
    }
}
